package mathive.client.menu;

import mathive.client.PanelHandler;
import mathive.client.UserHandler;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class MenuPanel extends VerticalPanel {
	private static int HOME = 0;
	private static int GAMES = 1;
	private static int TESTS = 2;
	private static int CONTACT = 3;
	
	private HorizontalPanel textPanel;
	private HorizontalPanel buttonPanel;
	
	private MenuButton homeButton;
	private MenuButton gamesButton;
	private MenuButton testsButton;
	private MenuButton contactButton;
	
	public MenuPanel() {
//		addStyleName("menupanel");
		textPanel = new HorizontalPanel();
		buttonPanel = new HorizontalPanel();
		
		
		homeButton = new MenuButton("homebutton.png", "HOME");
		homeButton.addClickHandler(new ButtonClickHandler(HOME));
		homeButton.addMouseOverHandler(new OverButtonHandler(HOME));
		homeButton.addMouseOutHandler(new OutButtonHandler(HOME));
		
		gamesButton = new MenuButton("gamebutton.png", "GAMES");
		gamesButton.addClickHandler(new ButtonClickHandler(GAMES));
		gamesButton.addMouseOverHandler(new OverButtonHandler(GAMES));
		gamesButton.addMouseOutHandler(new OutButtonHandler(GAMES));
		
		testsButton = new MenuButton("testbutton.png", "TESTS");
		testsButton.addClickHandler(new ButtonClickHandler(TESTS));
		testsButton.addMouseOverHandler(new OverButtonHandler(TESTS));
		testsButton.addMouseOutHandler(new OutButtonHandler(TESTS));
		
		contactButton = new MenuButton("mailbutton.png", "CONTACT");
		contactButton.addClickHandler(new ButtonClickHandler(CONTACT));
		contactButton.addMouseOverHandler(new OverButtonHandler(CONTACT));
		contactButton.addMouseOutHandler(new OutButtonHandler(CONTACT));
		
		add(homeButton);
		add(gamesButton);
		add(testsButton);
		add(contactButton);
		
		add(textPanel);
		add(buttonPanel);
		
//		textPanel.addStyleName("textpanel");
//		buttonPanel.addStyleName("buttonpanel");
		VerticalPanel outertext = new VerticalPanel();
		VerticalPanel outerbutton = new VerticalPanel();
		outertext.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		outerbutton.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		outertext.setWidth("100%");
		outerbutton.setWidth("100%");
		outertext.add(textPanel);
		outerbutton.add(buttonPanel);
		
		RootPanel.get("textmenu").add(outertext);
		RootPanel.get("buttonmenu").add(outerbutton);
		
//		buttonPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
//		textPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
	}
	
	public void add(Widget w) {
		if (w instanceof MenuButton) {
			textPanel.add(((MenuButton)w).getText());
			buttonPanel.add(w);
		} else {
			super.add(w);
		}
	}
	
	private class ButtonClickHandler implements ClickHandler {
		private int mButton;
		
		public ButtonClickHandler(int button) {
			mButton = button;
		}
		
		public void onClick(ClickEvent event) {
			if (mButton == HOME) {
				PanelHandler.showHome();
			} else if (mButton == GAMES) {
				PanelHandler.showGames();
			} else if (mButton == TESTS) {
				PanelHandler.showTests();
			} else if (mButton == CONTACT) {
				PanelHandler.showContact();
			}
		}
	}
	
	private class OverButtonHandler implements MouseOverHandler {
		private int mButton;
		
		public OverButtonHandler(int button) {
			mButton = button;
		}
		
		public void onMouseOver(MouseOverEvent event) {
			if (mButton == HOME) {
				homeButton.textUp();
			} else if (mButton == GAMES) {
				gamesButton.textUp();
			} else if (mButton == TESTS) {
				testsButton.textUp();
			} else if (mButton == CONTACT) {
				contactButton.textUp();
			}
		}
	}
	
	private class OutButtonHandler implements MouseOutHandler {
		private int mButton;
		
		public OutButtonHandler(int button) {
			mButton = button;
		}

		@Override
		public void onMouseOut(MouseOutEvent event) {
			if (mButton == HOME) {
				homeButton.textDown();
			} else if (mButton == GAMES) {
				gamesButton.textDown();
//				PanelHandler.showCreateTest();
			} else if (mButton == TESTS) {
				testsButton.textDown();
			} else if (mButton == CONTACT) {
				contactButton.textDown();
			}
		}
	}
}
